<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>反转二叉树：不使用额外空间的优雅实现</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 8px;
            overflow-x: auto;
        }
        
        .code-block pre {
            margin: 0;
            padding: 20px;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        .tree-visualization {
            background: #f8f9fa;
            border-radius: 12px;
            padding: 2rem;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    反转二叉树
                </h1>
                <p class="text-xl md:text-2xl mb-8 opacity-90">
                    不使用额外空间的优雅实现
                </p>
                <div class="flex justify-center space-x-4">
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-filter backdrop-blur-sm">
                        <i class="fas fa-code mr-2"></i>
                        算法精讲
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-filter backdrop-blur-sm">
                        <i class="fas fa-tree mr-2"></i>
                        数据结构
                    </div>
                    <div class="bg-white bg-opacity-20 rounded-lg px-6 py-3 backdrop-filter backdrop-blur-sm">
                        <i class="fas fa-brain mr-2"></i>
                        思维训练
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction Section -->
    <section class="py-16">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto">
                <div class="content-card rounded-2xl shadow-xl p-8 md:p-12">
                    <p class="text-lg leading-relaxed text-gray-700 mb-6">
                        <span class="drop-cap">作</span>为数据结构中的一种重要形式，二叉树在计算机科学中扮演着举足轻重的角色。在实际开发中，我们常常需要对二叉树进行各种操作，而反转二叉树是其中一个经典且具有挑战性的操作。
                    </p>
                    <p class="text-lg leading-relaxed text-gray-700">
                        什么是反转二叉树呢？简单来说，就是将树的左右子树进行交换，使得原本左子树的节点都移到右边，而右子树的节点都移到左边。这个过程听起来简单，但在实现时却有其复杂性，尤其是我们希望在不使用额外空间的情况下完成这个操作。
                    </p>
                    
                    <!-- Tree Visualization -->
                    <div class="tree-visualization mt-8">
                        <h3 class="text-center text-xl font-semibold mb-4 text-gray-800">反转前后对比</h3>
                        <div class="mermaid">
                            graph TD
                                subgraph "反转前"
                                    A1[1] --> B1[2]
                                    A1 --> C1[3]
                                    B1 --> D1[4]
                                    B1 --> E1[5]
                                end
                                subgraph "反转后"
                                    A2[1] --> C2[3]
                                    A2 --> B2[2]
                                    B2 --> E2[5]
                                    B2 --> D2[4]
                                end
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Binary Tree Basics -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="section-title text-3xl font-bold mb-12 text-gray-800">二叉树的基本概念</h2>
                
                <div class="grid md:grid-cols-3 gap-8">
                    <div class="content-card rounded-xl shadow-lg p-6">
                        <div class="feature-icon">
                            <i class="fas fa-sitemap"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3 text-gray-800">定义与结构</h3>
                        <p class="text-gray-600">二叉树是一种递归定义的数据结构，每个节点最多有两个子节点，分别称为左子节点和右子节点。</p>
                    </div>
                    
                    <div class="content-card rounded-xl shadow-lg p-6">
                        <div class="feature-icon">
                            <i class="fas fa-layer-group"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3 text-gray-800">特性与分类</h3>
                        <p class="text-gray-600">包括满二叉树、完全二叉树、平衡二叉树、二叉搜索树等多种类型，每种都有其独特的性质和应用场景。</p>
                    </div>
                    
                    <div class="content-card rounded-xl shadow-lg p-6">
                        <div class="feature-icon">
                            <i class="fas fa-rocket"></i>
                        </div>
                        <h3 class="text-xl font-semibold mb-3 text-gray-800">应用场景</h3>
                        <p class="text-gray-600">广泛应用于数据存储、搜索排序、表达式解析、编译器设计等领域，是计算机科学的基础数据结构。</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Implementation Section -->
    <section class="py-16">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">
                <h2 class="section-title text-3xl font-bold mb-12 text-gray-800">具体实现</h2>
                
                <div class="grid md:grid-cols-2 gap-8">
                    <!-- Recursive Implementation -->
                    <div class="content-card rounded-xl shadow-lg p-8">
                        <h3 class="text-2xl font-semibold mb-6 text-gray-800">
                            <i class="fas fa-sync-alt text-purple-600 mr-3"></i>递归实现
                        </h3>
                        <p class="text-gray-600 mb-6">递归方法利用函数调用栈，代码简洁优雅，思路清晰。通过递归地反转左右子树，然后交换它们的位置。</p>
                        
                        <div class="code-block">
                            <pre><code>public TreeNode invertTree(TreeNode root) {
    // 基本情况：空节点直接返回
    if (root == null) {
        return null;
    }
    
    // 递归反转左右子树
    TreeNode left = invertTree(root.left);
    TreeNode right = invertTree(root.right);
    
    // 交换左右子树
    root.left = right;
    root.right = left;
    
    return root;
}</code></pre>
                        </div>
                    </div>
                    
                    <!-- Iterative Implementation -->
                    <div class="content-card rounded-xl shadow-lg p-8">
                        <h3 class="text-2xl font-semibold mb-6 text-gray-800">
                            <i class="fas fa-redo text-purple-600 mr-3"></i>迭代实现
                        </h3>
                        <p class="text-gray-600 mb-6">迭代方法使用栈来模拟递归过程，避免了递归调用的栈空间消耗，适合处理深度较大的树。</p>
                        
                        <div class="code-block">
                            <pre><code>public TreeNode invertTree(TreeNode root) {
    if (root == null) return null;
    
    Stack&lt;TreeNode&gt; stack = new Stack&lt;&gt;();
    stack.push(root);
    
    while (!stack.isEmpty()) {
        TreeNode node = stack.pop();
        
        // 交换左右子树
        TreeNode temp = node.left;
        node.left = node.right;
        node.right = temp;
        
        // 将子节点入栈
        if (node.left != null) stack.push(node.left);
        if (node.right != null) stack.push(node.right);
    }
    
    return root;
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Complexity Analysis -->
    <section class="py-16 bg-gray-50">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto">
                <h2 class="section-title text-3xl font-bold mb-12 text-gray-800">复杂度分析</h2>
                
                <div class="content-card rounded-xl shadow-lg p-8">
                    <div class="grid md:grid-cols-2 gap-8">
                        <div class="text-center">
                            <div class="inline-flex items-center justify-center w-24 h-24 bg-purple-100 rounded-full mb-4">
                                <i class="fas fa-clock text-3xl text-purple-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold mb-2 text-gray-800">时间复杂度</h3>
                            <p class="text-3xl font-bold text-purple-600 mb-2">O(N)</p>
                            <p class="text-gray-600">需要访问每个节点一次，N为节点总数</p>
                        </div>
                        
                        <div class="text-center">
                            <div class="inline-flex items-center justify-center w-24 h-24 bg-purple-100 rounded-full mb-4">
                                <i class="fas fa-memory text-3xl text-purple-600"></i>
                            </div>
                            <h3 class="text-xl font-semibold mb-2 text-gray-800">空间复杂度</h3>
                            <p class="text-3xl font-bold text-purple-600 mb-2">O(H)</p>
                            <p class="text-gray-600">H为树的高度，最坏情况下为O(N)</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Applications -->
    <section class="py-16">
        <div class="container mx-auto px-6">
            <div class="max-w-6xl mx-auto">